package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Website;

/**
 * 网站基本信息实现类
 */
@Service
@Transactional
public class WebsiteService extends BaseDao<Website> {
	
	/**
	 * 按条件查询
	 * @param sp
	 * 
	 * @return
	 */
	public List<Website> query(Website sp){
		if(sp==null) return super.findAll();
		if(sp.getiDisplayLength() > 0){
	     return super.find(createFilter(sp),sp.getiDisplayStart(),sp.getiDisplayLength());
			}
		return super.find(createFilter(sp));
	}
	/**
	 * 拼装条件
	 * @param sp
	 * 
	 * @return
	 */
	private DetachedCriteria createFilter(Website sp){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(sp.getName())) {
	        dc.add(Restrictions.like("name", sp.getName(),MatchMode.ANYWHERE));
		} 
		if(!StringUtil.isNullOrEmpty(sp.getUrl())) {
	        dc.add(Restrictions.like("url", sp.getUrl(),MatchMode.ANYWHERE));
		} 
	 
		dc.addOrder(Order.desc("createTime"));
			return dc;
	}
}